import { Bmobs } from "../plugins/bmob";
import { Service } from "typedi";

@Service()
export class CarDao extends Bmobs {
	
	constructor() {
		super()
	}
	
	/**
	 * 更新用户的订单状态
	 * @param orderNumber
	 * @param userid
	 */
	public async updateOrderStatus(orderNumber: number, userid: string): Promise<Object> {
		this.setTableName("couresCar");
		
		this.query.equalTo("userid", "==", userid);
		this.query.equalTo("orderNumber", "==", orderNumber);
		
		let orderList = await this.query.find();
		
		orderList.forEach(async val => {
			let currentData =	await this.query.get(val['objectId']);
			currentData.set('orderStatus', 1);
			currentData.save()
		});
		
		return { s: '更新订单成功' }
	}
	
	/**
	 * 添加课程到购物车，但是先检查一下用户是否已经添加过，杜绝重复添加
	 * @param userid
	 * @param courseId
	 */
	public async addCarList (userid: string, courseId: string): Promise<Object> {

		this.setTableName("couresCar")
		this.query.equalTo("courseId", "==", courseId);
		this.query.equalTo("userid", "==", userid);
		
		let addHave = await this.query.find();

		if(addHave.length != 0) {
			return addHave[0];
		} else {
			this.setTableName("couresCar")
			this.query.set("userid", userid)
			this.query.set("courseId", courseId)
			this.query.set("orderStatus", 0)
			return new Promise((resolve, reject) => {
				this.query.save().then(res => {
					resolve(res)
				}).catch(err => {
					this.log.logSymbolsError(err.message)
				})
			});
		}
		
	}
	
	public async getCarPayList (userid: string): Promise<Array<Object>> {
		this.setTableName("couresCar");
		this.query.equalTo("userid", "==", userid);
		this.query.equalTo("orderStatus", "==", 1);
		let couresDataList = [];
		
		let Find = await this.query.find();
		
		return new Promise(async (resolve, reject) => {
			for(let i = 0; i < Find.length; i++) {
				this.setTableName("coures");
				this.query.equalTo("objectId", "==", Find[i].courseId);
				let list = (await this.query.find())[0];
				couresDataList.push(list)
			}
			resolve(couresDataList)
		});
	}
	
	public async getCarList (userid: string, OrderNumber: number): Promise<Array<Object>> {
		this.setTableName("couresCar");
		this.query.equalTo("userid", "==", userid);
		this.query.equalTo("orderStatus", "==", 0);
		let couresDataList = [];
		
		let Find = await this.query.find();
		
		
		this.setTableName("couresCar");
		
		Find.forEach(async val => {
		  let currentData =	await this.query.get(val['objectId'])
			currentData.set('orderNumber', OrderNumber)
			currentData.save()
		})
		
		
		return new Promise(async (resolve, reject) => {
			for(let i = 0; i < Find.length; i++) {
				this.setTableName("coures");
				this.query.equalTo("objectId", "==", Find[i].courseId);
				let list = (await this.query.find())[0];
				couresDataList.push(list)
			}
			resolve(couresDataList)
		});
	}
	
	/**
	 * 删除购物车中的记录
	 * @param userid
	 * @param courseid
	 */
	public async deleteCar(userid: string, courseid: string): Promise<Object> {
		this.setTableName("couresCar")
		this.query.set("userid", userid)
		this.query.set("courseId", courseid)
		let Car = (await this.query.find())[0]
		console.log(Car)
		return new Promise((resolve, reject) => {
			this.query.destroy(Car['objectId']).then(res => {
				resolve(res)
			}).catch(err => {
				this.log.logSymbolsError(err.message)
			})
		})
	}
}